﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;

namespace InventoryPro.DAL
{
    public class EmployeeDAO
    {
        InvEntities context ;
        public EmployeeDAO() 
        {
             
        context = new InvEntities(); 
        }

        public List<Employee> GetAllEmployees()
        {

            
            var query = from c in context.Employees                       
                        select c;
 
            //var queryResult = (((ObjectQuery)query).Execute(MergeOption.AppendOnly));
            //             context.Dispose();

           return  query.ToList<Employee>();
            
         }

        public Employee  GetEmployeebyID(int ID)

        {
            Employee employee = null;
            var query = from e in context.Employees
                        where e.EmployeeID == ID
                        select e;
            var Q = (((ObjectQuery)query).Execute(MergeOption.AppendOnly));
            foreach (Employee e in Q)
            {
                employee = e;                
            }

            if(employee != null)
            context.Detach(employee);
            return employee;
        }


        public void CreateEmployee(Employee employee)

        {
            context.AddToEmployees(employee);
            context.SaveChanges();

        }


        public void UpdateEmployee(Employee employee)

        {

            context.Attach(employee);
             context.SaveChanges();
        }


        public void DeleteEmployee(int ID)
        {
            Employee employee = null;
            var query = from e in context.Employees
                        where e.EmployeeID == ID
                        select e;
            var Q = (((ObjectQuery)query).Execute(MergeOption.AppendOnly));
            foreach (Employee e in Q)
            {
                employee = e;                
            }

            context.DeleteObject(employee);
            
        }





        public Employee Login(string LoginName, string pwd)
        {
            return context.Employees.Single(emp => emp.LoginName == LoginName && emp.Password == pwd);
        }
    }
}




